<template>
  <div ref="vantaRef" class="login-container medical-system">
    <div class="login-form-container">
      <el-card class="login-card glass-effect scale-in" shadow="never">
        <template #header>
          <div class="card-header">
            <div class="logo-section">
              <div class="logo-glow">
                <img
                  src="https://picsum.photos/60"
                  alt="系统Logo"
                  class="login-logo"
                />
              </div>
            </div>
            <h2 class="login-title">溃疡性结肠炎智能评分系统</h2>
            <p class="login-subtitle">EndoSight-UC 医生工作站</p>
          </div>
        </template>

        <el-form
          ref="loginFormRef"
          :model="loginForm"
          :rules="loginRules"
          class="login-form"
          size="large"
          @submit.prevent="handleLogin"
        >
          <el-form-item prop="username">
            <el-input
              v-model="loginForm.username"
              placeholder="请输入工号"
              prefix-icon="User"
              clearable
              :disabled="loading"
            />
          </el-form-item>

          <el-form-item prop="password">
            <el-input
              v-model="loginForm.password"
              type="password"
              placeholder="请输入密码"
              prefix-icon="Lock"
              show-password
              :disabled="loading"
              @keyup.enter="handleLogin"
            />
          </el-form-item>

          <el-form-item class="login-button">
            <el-button
              type="primary"
              size="large"
              :loading="loading"
              @click="handleLogin"
              class="submit-button"
            >
              <span v-if="!loading">登录</span>
              <span v-else>登录中...</span>
            </el-button>
          </el-form-item>
        </el-form>

        <div class="login-footer">
          <el-divider>
            <span class="footer-text">System Information</span>
          </el-divider>
          <div class="test-info">
            <p><strong>System:</strong> EndoSight-UC Medical AI</p>
            <p><strong>Status:</strong> Secure</p>
            <p><small>Contact administrator for credentials</small></p>
          </div>
        </div>
      </el-card>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted, onBeforeUnmount } from 'vue'
import { useStore } from 'vuex'
import { useRouter, useRoute } from 'vue-router'
import { ElMessage, ElForm } from 'element-plus'
import { User, Lock } from '@element-plus/icons-vue'
import * as THREE from 'three'
import WAVES from 'vanta/src/vanta.waves'

interface LoginForm {
  username: string
  password: string
}

const store = useStore()
const router = useRouter()
const route = useRoute()

// Vanta背景引用
const vantaRef = ref<HTMLElement>()
let vantaEffect: any = null

// 表单引用
const loginFormRef = ref<InstanceType<typeof ElForm>>()

// 加载状态
const loading = ref(false)

// 登录表单数据
const loginForm = reactive<LoginForm>({
  username: '',
  password: ''
})

// 表单验证规则
const loginRules = {
  username: [
    { required: true, message: '请输入工号', trigger: 'blur' },
    { min: 2, max: 20, message: '工号长度在 2 到 20 个字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 20, message: '密码长度在 6 到 20 个字符', trigger: 'blur' }
  ]
}

// 处理登录
const handleLogin = async () => {
  if (!loginFormRef.value) return

  try {
    const valid = await loginFormRef.value.validate()
    if (!valid) return

    loading.value = true

    // 调用登录API
    const result = await store.dispatch('auth/login', loginForm)

    if (result.success) {
      ElMessage.success('登录成功')

      // 获取重定向路径
      const redirectPath = (route.query.redirect as string) || '/scoring/new'

      // 延迟跳转，让用户看到成功提示
      setTimeout(() => {
        router.push(redirectPath)
      }, 1000)
    } else {
      ElMessage.error(result.message || '登录失败')
    }
  } catch (error) {
    console.error('Login error:', error)
    ElMessage.error('登录失败，请稍后重试')
  } finally {
    loading.value = false
  }
}

// 页面加载时初始化Vanta背景
onMounted(() => {
  // 安全修复：移除自动填充功能
  // 生产环境不允许自动填充凭据

  // 初始化Vanta动态背景
  if (vantaRef.value) {
    vantaEffect = WAVES({
      el: vantaRef.value,
      THREE: THREE,
      color: 0x1e3a5f,
      waveHeight: 20,
      waveSpeed: 1.2,
      zoom: 0.8
    })
  }
})

// 清理Vanta效果
onBeforeUnmount(() => {
  if (vantaEffect) {
    vantaEffect.destroy()
  }
})
</script>

<style scoped>
.login-container {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  position: relative;
  overflow: hidden;
}

.login-form-container {
  width: 100%;
  max-width: 420px;
  z-index: 10;
  position: relative;
}

/* 毛玻璃登录卡效果 */
.login-card {
  border-radius: 20px;
  overflow: hidden;
  border: 1px solid rgba(255, 255, 255, 0.1);
  background: rgba(255, 255, 255, 0.05);
  backdrop-filter: blur(18px) saturate(150%);
  -webkit-backdrop-filter: blur(18px) saturate(150%);
  box-shadow:
    0 20px 40px rgba(0, 0, 0, 0.1),
    0 0 0 1px rgba(255, 255, 255, 0.05) inset;
}

:deep(.el-card__header) {
  background: linear-gradient(135deg,
    rgba(102, 126, 234, 0.8) 0%,
    rgba(118, 75, 162, 0.8) 100%);
  backdrop-filter: blur(10px);
  padding: 40px 30px 30px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.1);
}

:deep(.el-card__body) {
  padding: 40px 30px;
  background: rgba(255, 255, 255, 0.02);
}

.card-header {
  text-align: center;
  color: white;
  position: relative;
}

.logo-section {
  margin-bottom: 20px;
  position: relative;
}

/* Logo辉光效果 */
.logo-glow {
  position: relative;
  display: inline-block;
}

.logo-glow::before {
  content: '';
  position: absolute;
  top: -10px;
  left: -10px;
  right: -10px;
  bottom: -10px;
  background: radial-gradient(circle, rgba(102, 126, 234, 0.3) 0%, transparent 70%);
  border-radius: 50%;
  animation: pulse 3s ease-in-out infinite;
  z-index: -1;
}

.login-logo {
  width: 60px;
  height: 60px;
  border-radius: 50%;
  border: 3px solid rgba(255, 255, 255, 0.3);
  background: white;
  padding: 8px;
  position: relative;
  z-index: 2;
  transition: all 0.3s ease;
}

.login-logo:hover {
  transform: scale(1.05) rotate(5deg);
  box-shadow: 0 10px 25px rgba(102, 126, 234, 0.4);
}

.login-title {
  font-size: 24px;
  font-weight: 700;
  margin: 0 0 8px 0;
  text-shadow: 0 2px 8px rgba(0, 0, 0, 0.3);
  letter-spacing: 0.5px;
}

.login-subtitle {
  font-size: 14px;
  opacity: 0.9;
  margin: 0;
  font-weight: 400;
  letter-spacing: 0.3px;
}

.login-form {
  margin-top: 20px;
}

:deep(.el-form-item) {
  margin-bottom: 24px;
}

/* 增强输入框辉光效果 */
:deep(.el-input__wrapper) {
  border-radius: 12px;
  padding: 14px 18px;
  background: rgba(255, 255, 255, 0.08);
  border: 1px solid rgba(255, 255, 255, 0.1);
  backdrop-filter: blur(10px);
  transition: all 0.4s cubic-bezier(0.4, 0, 0.2, 1);
  position: relative;
}

:deep(.el-input__wrapper:hover) {
  background: rgba(255, 255, 255, 0.12);
  border-color: rgba(102, 126, 234, 0.3);
  transform: translateY(-1px);
  box-shadow: 0 8px 25px rgba(102, 126, 234, 0.15);
}

:deep(.el-input__wrapper.is-focus) {
  background: rgba(255, 255, 255, 0.15);
  border-color: rgba(102, 126, 234, 0.6);
  box-shadow:
    0 0 0 3px rgba(102, 126, 234, 0.1),
    0 0 20px rgba(102, 126, 234, 0.3),
    0 8px 30px rgba(102, 126, 234, 0.2);
  transform: translateY(-2px);
}

:deep(.el-input__inner) {
  color: #333;
  font-weight: 500;
}

:deep(.el-input__inner::placeholder) {
  color: #666;
  opacity: 0.7;
}

.login-button {
  margin-top: 30px;
  margin-bottom: 0;
}

/* 超级按钮效果 */
.submit-button {
  width: 100%;
  height: 52px;
  border-radius: 12px;
  font-size: 16px;
  font-weight: 700;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border: none;
  position: relative;
  overflow: hidden;
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  letter-spacing: 0.5px;
  text-transform: uppercase;
}

.submit-button::before {
  content: '';
  position: absolute;
  top: 0;
  left: -100%;
  width: 100%;
  height: 100%;
  background: linear-gradient(90deg,
    transparent 0%,
    rgba(255, 255, 255, 0.3) 50%,
    transparent 100%);
  transition: left 0.6s ease;
}

.submit-button:hover::before {
  left: 100%;
}

.submit-button:hover {
  transform: translateY(-3px) scale(1.02);
  box-shadow:
    0 15px 35px rgba(102, 126, 234, 0.4),
    0 0 25px rgba(118, 75, 162, 0.3);
  background: linear-gradient(135deg, #7a8ff0 0%, #8a5bb2 100%);
}

.submit-button:active {
  transform: translateY(-1px) scale(0.98);
  box-shadow:
    0 8px 20px rgba(102, 126, 234, 0.3),
    0 0 15px rgba(118, 75, 162, 0.2);
}

.login-footer {
  margin-top: 30px;
}

.footer-text {
  font-size: 12px;
  color: rgba(255, 255, 255, 0.7);
}

:deep(.el-divider__text) {
  background-color: rgba(255, 255, 255, 0.05);
  padding: 0 16px;
  backdrop-filter: blur(10px);
  border-color: rgba(255, 255, 255, 0.1);
}

.test-info {
  background: rgba(255, 255, 255, 0.08);
  border-radius: 12px;
  padding: 16px;
  border-left: 4px solid rgba(102, 126, 234, 0.7);
  backdrop-filter: blur(10px);
  border: 1px solid rgba(255, 255, 255, 0.1);
}

.test-info p {
  margin: 4px 0;
  font-size: 13px;
  color: rgba(255, 255, 255, 0.8);
}

.test-info strong {
  color: rgba(255, 255, 255, 0.95);
  font-weight: 600;
}

/* 动画效果 */
@keyframes pulse {
  0%, 100% {
    opacity: 0.3;
    transform: scale(1);
  }
  50% {
    opacity: 0.6;
    transform: scale(1.1);
  }
}

@keyframes fadeInUp {
  from {
    opacity: 0;
    transform: translateY(40px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@keyframes glow {
  0%, 100% {
    box-shadow: 0 0 20px rgba(102, 126, 234, 0.3);
  }
  50% {
    box-shadow: 0 0 35px rgba(102, 126, 234, 0.6);
  }
}

.login-card {
  animation: fadeInUp 0.8s cubic-bezier(0.4, 0, 0.2, 1);
}

.login-form-container:hover .login-card {
  animation: glow 3s ease-in-out infinite;
}

/* 响应式设计 */
@media (max-width: 480px) {
  .login-form-container {
    max-width: 100%;
    margin: 0 20px;
  }

  :deep(.el-card__header),
  :deep(.el-card__body) {
    padding: 30px 20px;
  }

  .login-title {
    font-size: 20px;
  }
}
</style>